// File Name: ws2812b_logic.c
// Author: kukukukuku~
// Date: 2024-06-26

#include "./User/WS2812B/inc/ws2812b_logic.h"

/**
 * @brief   向WS2812B发送一个位
 * @param   bit:    要发送的位
 * @retval  None
 * @note    静态函数
*/
static void ws2812b_send_bit(uint8_t bit)
{
    while(!spi_i2s_flag_get(WS2812B_SPI, SPI_I2S_TDBE_FLAG));
    bit ?\
    spi_i2s_data_transmit(WS2812B_SPI, WS2812B_HIGH) :\
    spi_i2s_data_transmit(WS2812B_SPI, WS2812B_LOW);
    while(!spi_i2s_flag_get(WS2812B_SPI, SPI_I2S_TDBE_FLAG));
}


/**
 * @brief   向WS2812B发送一个字节
 * @param   byte:   要发送的字节
 * @retval  None
 * @note    静态函数
*/
static void ws2812b_send_byte(uint8_t byte)
{
    for(uint8_t i = 0; i < 8; i++)
    {
        ws2812b_send_bit(byte & (0x80 >> i));
    }
}


/**
 * @brief   向WS2812B发送复位信号
 * @param   None
 * @retval  None
 * @note    None
*/
void ws2812b_send_reset(void)
{
    for(uint16_t i = 0; i < 300; i++)
    {
        while(!spi_i2s_flag_get(WS2812B_SPI, SPI_I2S_TDBE_FLAG));
        spi_i2s_data_transmit(WS2812B_SPI, WS2812B_RESET);
        while(!spi_i2s_flag_get(WS2812B_SPI, SPI_I2S_TDBE_FLAG));
    }
}


/**
 * @brief   向WS2812B发送颜色数据
 * @param   ws2812b:    WS2812B对象
 * @retval  None
 * @note    None
*/
void ws2812b_send_rgb_data(pWS2812B_RGB ws2812b)
{
    ws2812b_send_byte(ws2812b->ws2812b.gData);
    ws2812b_send_byte(ws2812b->ws2812b.rData);
    ws2812b_send_byte(ws2812b->ws2812b.bData);
}
